/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JOrderPaymentsPanel.java
 *
 * Created on 2010-4-24, 8:22:56
 */

package cn.ekuma.epos.qkorder.link;

import cn.ekuma.epos.bean.util.PaymentInfoHelper;
import cn.ekuma.epos.qkorder.event.OrderChangedEvent;
import cn.ekuma.epos.qkorder.event.OrderChangedListener;

import com.openbravo.bean.erp.Order;
import javax.swing.table.AbstractTableModel;

import com.openbravo.pos.base.AppLocal;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author Administrator
 */
public class JOrderPaymentsPanel extends javax.swing.JPanel implements OrderChangedListener{
    Order order;
    PaymentInfoTableModel payModel;
    private final static String[] PAYMENTHEADERS = {"Label.Payment", "label.totalcash"};

    /** Creates new form JOrderPaymentsPanel */
    public JOrderPaymentsPanel() {
        initComponents();
        payModel=new PaymentInfoTableModel();
        jPayTable.setModel(payModel);
        jPayTable.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jPayTable = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jPayTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jPayTable.setFocusable(false);
        jPayTable.setVerifyInputWhenFocusTarget(false);
        jScrollPane1.setViewportView(jPayTable);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void orderChanged(OrderChangedEvent event) {
        this.order=event.getOrder();
        payModel.fireTableDataChanged();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable jPayTable;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    class PaymentInfoTableModel extends AbstractTableModel{

        @Override
         public String getColumnName(int column) {
                return AppLocal.getIntString(PAYMENTHEADERS[column]);
          }

        public int getRowCount() {
            if(order==null)
               return 0;
            return  order.getPayments().size();
        }

        public int getColumnCount() {
            return 2;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            switch(columnIndex){
                case 0:
                     return AppLocal.getIntString("transpayment." + order.getPayments().get(rowIndex).getName());
                case 1:
                     return PaymentInfoHelper.printTotal(order.getPayments().get(rowIndex));
            }
           return null;
        }
    }
}
